package com.csc.finalproject.datacontroller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csc.finalproject.domain.AccountInfo;
import com.csc.finalproject.service.AccountInfoService;

@Controller
@RequestMapping("/json/accountinfo")
public class AccountDataController {
	@Autowired
	AccountInfoService accountService;

	@RequestMapping(value = "/getAllAccount", method = RequestMethod.POST)
	@ResponseBody
	public JSONArray getShopInJSON() {
		JSONArray listAccount = new JSONArray();
		List<AccountInfo> accounts = accountService.getAccountInfos();
		for (AccountInfo accountInfo : accounts) {
			JSONObject obj = new JSONObject();
			obj.put("accountIdEdit", accountInfo.getAccountId());
			obj.put("accountNumber", accountInfo.getAccountNumber());
			obj.put("identityCard", accountInfo.getIdentifyCard());
			obj.put("firstName", accountInfo.getFirstName());
			obj.put("lastName", accountInfo.getLastName());
			obj.put("midName", accountInfo.getMidName());
			obj.put("phone1", accountInfo.getPhone1());
			obj.put("phone2", accountInfo.getPhone2());
			obj.put("email1", accountInfo.getEmail1());
			obj.put("email2", accountInfo.getEmail2());
			obj.put("state", accountInfo.getState().getStateId());
			obj.put("stateName", accountInfo.getState().getStateName());
			listAccount.add(obj);
		}
		return listAccount;
	}

	@RequestMapping(value = "/getAccountById/{name}", method = RequestMethod.GET)
	public @ResponseBody JSONObject getAccountById(@PathVariable String name) {
		JSONObject obj = new JSONObject();
		AccountInfo account = accountService.getAccountInfo(Integer
				.parseInt(name));
		obj.put("accountId", account.getAccountId());
		obj.put("accountNumber", account.getAccountNumber());
		obj.put("identityCard", account.getIdentifyCard());
		obj.put("firstName", account.getFirstName());
		obj.put("lastName", account.getLastName());
		obj.put("midName", account.getMidName());
		obj.put("phone1", account.getPhone1());
		obj.put("phone2", account.getPhone2());
		obj.put("email1", account.getEmail1());
		obj.put("email2", account.getEmail2());
		obj.put("state", account.getState().getStateId());
		obj.put("stateName", account.getState().getStateName());
		return obj;
	}

	@RequestMapping(value = "/updateAccountInfo", method = RequestMethod.POST)
	public @ResponseBody boolean updateAccountInfo(
			@RequestParam("accountIdEdit") String accountIdEdit,
			@RequestParam("accountNumberEdit") String accountNumberEdit,
			@RequestParam("identityCardEdit") String identityCardEdit,
			@RequestParam("firstNameEdit") String firstNameEdit,
			@RequestParam("midNameEdit") String midNameEdit,
			@RequestParam("lastNameEdit") String lastNameEdit,
			@RequestParam("phone1Edit") String phone1Edit,
			@RequestParam("phone2Edit") String phone2Edit,
			@RequestParam("email1Edit") String email1Edit,
			@RequestParam("email2Edit") String email2Edit,
			@RequestParam("stateEdit") String stateEdit,
			HttpServletRequest request) {
		accountService.editAccountInfo(Integer.parseInt(accountIdEdit),
				accountNumberEdit, identityCardEdit, firstNameEdit,
				lastNameEdit, midNameEdit, phone1Edit, phone2Edit, email1Edit,
				email2Edit, Integer.parseInt(stateEdit));
		return true;
	}

	@RequestMapping(value = "/getAccountsByState", method = RequestMethod.POST)
	public @ResponseBody JSONArray getAccountsByState(
			@RequestParam("stateId") String stateId, HttpServletRequest request) {
		JSONArray listAccount = new JSONArray();
		List<AccountInfo> accounts = accountService.getAccountsByState(Integer
				.parseInt(stateId));
		for (AccountInfo accountInfo : accounts) {
			JSONObject obj = new JSONObject();
			obj.put("accountId", accountInfo.getAccountId());
			obj.put("accountNumber", accountInfo.getAccountNumber());
			obj.put("firstName", accountInfo.getFirstName());
			obj.put("lastName", accountInfo.getLastName());
			obj.put("state", accountInfo.getState().getStateId());
			listAccount.add(obj);
		}
		return listAccount;
	}

	@RequestMapping(value = "/transferState", method = RequestMethod.POST)
	public @ResponseBody boolean transferToActive(
			@RequestParam("accountId") String accountId,
			@RequestParam("stateId") String stateId, HttpServletRequest request) {
		accountService.transferState(accountId, stateId);
		return true;
	}

	@RequestMapping(value = "/removeAccountInfo", method = RequestMethod.POST)
	public @ResponseBody boolean removeAccount(
			@RequestParam("accountId") String accountId) {
		accountService.removeAccountInfo(accountId);
		return true;
	}

	@RequestMapping(value = "/addNewAccount", method = RequestMethod.POST)
	public @ResponseBody boolean addNewAccount(
			@RequestParam("accountNumber") String accountNumber,
			@RequestParam("identityCard") String identityCard,
			@RequestParam("firstName") String firstName,
			@RequestParam("midName") String midName,
			@RequestParam("lastName") String lastName,
			@RequestParam("phone1") String phone1,
			@RequestParam("phone2") String phone2,
			@RequestParam("email1") String email1,
			@RequestParam("email2") String email2, HttpServletRequest request) {
		accountService.addAccountInfo(accountNumber, identityCard, firstName,
				lastName, midName, phone1, phone2, email1, email2, 1);
		return true;
	}

	@RequestMapping(value = "/searchAccountInfo", method = RequestMethod.POST)
	public @ResponseBody JSONArray searchAccountInfoSimple(
			@RequestParam("txtSearch") String txtSearch,
			HttpServletRequest request) {
		System.out.println(txtSearch);
		JSONArray listAccount = new JSONArray();
		List<AccountInfo> accounts = accountService.search(txtSearch);
		System.out.println(accounts.size());

		for (AccountInfo account : accounts) {
			JSONObject obj = new JSONObject();
			obj.put("accountId", account.getAccountId());
			obj.put("accountNumber", account.getAccountNumber());
			obj.put("identityCard", account.getIdentifyCard());
			obj.put("firstName", account.getFirstName());
			obj.put("lastName", account.getLastName());
			obj.put("midName", account.getMidName());
			obj.put("phone1", account.getPhone1());
			obj.put("phone2", account.getPhone2());
			obj.put("email1", account.getEmail1());
			obj.put("email2", account.getEmail2());
			obj.put("state", account.getState().getStateId());
			listAccount.add(obj);
		}
		return listAccount;
	}

	@RequestMapping(value = "/searchAdvance", method = RequestMethod.POST)
	public @ResponseBody JSONArray searchAdvance(
			@RequestParam("accountNumber") String accountNumber,
			@RequestParam("identifyCard") String identifyCard,
			@RequestParam("firstName") String firstName,
			@RequestParam("lastName") String lastName,
			@RequestParam("phone") String phone,
			@RequestParam("stateId") String stateId, HttpServletRequest request) {
		JSONArray listAccount = new JSONArray();

		List<AccountInfo> accounts = accountService.searchAdvance(
				accountNumber, identifyCard, firstName, lastName, phone,
				stateId);
		for (AccountInfo account : accounts) {
			JSONObject obj = new JSONObject();
			obj.put("accountId", account.getAccountId());
			obj.put("accountNumber", account.getAccountNumber());
			obj.put("identityCard", account.getIdentifyCard());
			obj.put("firstName", account.getFirstName());
			obj.put("lastName", account.getLastName());
			obj.put("midName", account.getMidName());
			obj.put("phone1", account.getPhone1());
			obj.put("phone2", account.getPhone2());
			obj.put("email1", account.getEmail1());
			obj.put("email2", account.getEmail2());
			obj.put("state", account.getState().getStateId());
			listAccount.add(obj);
		}
		return listAccount;
	}

	@RequestMapping(value = "/changePage", method = RequestMethod.POST)
	public @ResponseBody String changePage() {
		return "";
	}

}
